<template>
	<view>
		<navigator class="left_box" v-if='order_id' :url="'/pages/order/detail/detail?order_id='+order_id">
			<text>查看定单详情</text>
		</navigator>
		<view class="score">
			<view class="hotelService">
				<view class="title">
					<text class="name">赠送积分</text>
				</view>
				<view class="content">
					<view class="box">
						<view class="">
							<text style="color: red;">*</text>
							<text class="txt1">赠送积分</text>
						</view>
						<input type="text" :disabled="isEdit" v-model="scoreForm.score" placeholder="请输入赠送的积分" />
					</view>
					<view class="box">
						<view class="">
							<text style="color: red;">*</text>
							<text class="txt1">赠送积分手机号</text>
						</view>
						<input type="text" :disabled="isEdit" v-model="scoreForm.mobile" placeholder="请输入要赠送的手机号" />
					</view>
				</view>
			</view>
		</view>

		<button type="default" class="order_btn" @click="saveFn()" v-if="options.api">保存</button>
		<divider title="审核流程"></divider>
		<!-- 新版审批流程————创建  -->
		<template v-if="sequenceData.length > 0">
			<steps-create :stepList="sequenceData"></steps-create>
		</template>
		<!-- 新版审核流程————查看 -->
		<template v-if="confirmSteps.length > 0">
			<steps-look :stepList="confirmSteps"></steps-look>
			<footer-action-btn :btnValue="buttons" :transferForm="transferForm" @clearTransferForm="clearTransferForm"
			 :isShowBtn="options.api?false:true" @resubmit='resubmits' @transferSubmit="transferSubmit"></footer-action-btn>
		</template>
	</view>
</template>

<script>
	import stepsLook from '@/component/steps-look/steps-look.vue' // 新版审核流程
	import footerActionBtn from '@/component/footer-action-btn/footer-action-btn.vue' // 底部操作按钮组
	import stepsCreate from '@/component/steps-create/steps-create.vue'
	import divider from '@/component/divider/divider'
	
	import request from '@/utils/request.js';
	export default {
		components: {
			stepsCreate,
			stepsLook,
			footerActionBtn,
			divider
		},
		data() {
			return {
				order_id: 0,
				confirm_id: 0,

				isEdit: false,
				options: '',

				buttons: [],

				scoreForm: {
					score: '',
					mobile: ''
				},
				
				transferForm: {
					realname: '',
					user_id: ''
				},
				sequenceData: [], // 新版审核
				confirmSteps: [], // 新版审核
			}
		},
		onLoad(options) {
			// 添加 从订单详情页添加子项目 包含order_id
			if (options.params) {
				this.options = JSON.parse(options.params);
				this.order_id = this.options.order_id;
				this.getConfirmSequence()
			}

			// 审核查看 lookApproval是个对象，包含的id是审核的编号，order_id要从审核详情中获取
			if (options.lookApproval) {
				this.options = JSON.parse(options.lookApproval)
				this.isEdit = true;
				//查看信息
				this.getDetail(this.options)
				this.confirm_id = this.options.id;
				// 审核流程--查看
				this.getConfirmList()
			}

			// 查看 从查看订单审核中进入只有id，这个id是审核的编号，order_id要从审核详情中获取
			if (options.id) {
				this.options = options;
				this.isEdit = true;
				//查看信息
				this.getDetail(this.options)
				this.confirm_id = this.options.id;
				// 审核流程--查看
				this.getConfirmList()
			}
		},
		methods: {
			getDetail(e) {
				let that = this
				let data
				if (e.is_comment) {
					data = {
						id: e.id,
						is_comment: e.is_comment,
						type: e.user ? 'confirm_user' : '',
					}

				} else {
					data = {
						id: e.id,
						type: e.user ? 'confirm_user' : '',
					}
				}
				request.request({
					url: '/h5/order.confirm/detail',
					showLoading: true,
					token: true,
					data: data,
					success(res) {
						if (res.code == 200) {
							that.scoreForm = res.data.detail;
							that.buttons = res.data.buttons;
							that.order_id = res.data.detail.order_id;
							//判断是否可以编辑
							that.buttons.map((item, index) => {
								if (item.id == 'update') {
									that.isEdit = false;
								}
							})
						}
					},
					fail(e) {
						uni.showToast({
							icon: "none",
							title: "服务器开小差去啦~",
						})
					}
				})
			},

			// 提交转交
			transferSubmit() {
				let that = this;
				request.request({
					url: '/api/order.audit/transfer',
					showLoading: true,
					token: true,
					data: {
						id: that.confirm_id,
						user_id: that.transferForm.user_id
					},
					success(res) {
						if (res.code == 200) {
							uni.showToast({
								icon: "none",
								title: res.msg
							})
							uni.navigateBack({})
						} else {
							uni.showToast({
								icon: "none",
								title: res.msg
							})
						}
					}
				})
			},
			// 点击取消时清除转交人员信息
			clearTransferForm() {
				this.transferForm = {
					realname: '',
					user_id: ''
				}
			},

			//查看详情审核流程
			getConfirmList() {
				let that = this
				request.request({
					url: "/api/order.audit/getAuditResult",
					showLoading: true,
					token: true,
					data: {
						id: that.confirm_id
					},
					success(res) {
						that.confirmSteps = res.data.process
					}
				})
			},
			//创建订单时的审核流程
			getConfirmSequence(e) {
				let that = this
				request.request({
					url: "/api/order.audit/getAuditProcess",
					showLoading: true,
					token: true,
					data: Object.assign({
						order_id: that.order_id,
						audit_type: 'score',
						company_id: 0
					}),
					success(res) {
						if (res.code == 200) {
							that.sequenceData = res.data.process
						} else {
							uni.showToast({
								icon: 'none',
								title: res.msg
							})
						}
					}
				})
			},

			//底部评论按钮
			resubmits(item) {
				if (item.label == '同意' || item.label == '拒绝' || item.label == '评论') {
					this.commentFn(item)
				} else if (item.label == '转交') {
					uni.navigateTo({
						url: '../../../component/search/search?type=changeUser'
					})
				} else {
					this.editFn(item.api, item.id)
				}

			},
			
			// 编辑提交
			commentFn(e){
				let that=this;
				let url = '';
				if(e.label == '拒绝'){
					url = '/api/order.audit/reject';
				}else if(e.label == '同意'){
					url = '/api/order.audit/accept'
				} else {
					url = e.api;
				}
				request.request({
					url: url,
					showLoading: true,
					token:true,
					data:{						
						order_id: that.order_id,
						id:that.confirm_id,						
						content:e.content?e.content:'',
						image:e.image?e.image:'',
					},
					success(res) {
					  if(res.code==200){													
							uni.showToast({
								icon:"none",
								title:res.msg
							})
				      that.getDetail(that.options)
							that.getConfirmList()
					  }else{
							 uni.showToast({
								icon:"none",
								title:res.msg
							 })	
					  }
					}
				})
			},
			
			// 编辑提交
			editFn(api,id){
				let that=this;
				let params = {};
				params['order_id'] = that.order_id;
				params['order_confirm_id'] = that.confirm_id;
				params['id'] = id?id:'';
				params = Object.assign(params, that.scoreForm);
				request.request({
					url: api,
					showLoading: true,
					token:true,
					data: params,
					success(res) {
					  if(res.code==200){							
							uni.redirectTo({
							 	url:"/pages/order/index"	
							})
							uni.showToast({
								icon:"none",
								title:res.msg
							})										
					  }else{
							 uni.showToast({
								icon:"none",
								title:res.msg
							 })	
					  }
					}
				})
			},

			// 保存
			saveFn() {
				let that = this;
				let params = that.scoreForm;
				console.log(that.order_id);
				if (params.mobile == '' || params.score == '') {
					uni.showToast({
						icon: 'none',
						title: '请填写完整'
					})
				} else {
					request.request({
						url: "/api/order.score/create",
						showLoading: true,
						token: true,
						data: Object.assign(params, {
							order_id: that.order_id
						}),
						success(res) {
							if (res.code == 200) {
								uni.navigateBack({})
							} else {
								uni.showToast({
									icon: 'none',
									title: res.msg
								})
							}
						}
					})
				}

			}

		}
	}
</script>

<style lang="less">
	@import '../common.less';
</style>
